<template>
  <div class="replace-box">
    <el-dialog width="500px" title="新增收款账户" v-model="dialogFormVisible" :draggable="true" @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item prop="showName" label="账户别名">
                <el-input style="width:90%;" v-model="dialogForm.showName" placeholder="请输入账户别名" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="accountType" label="账户类型">
                <el-input style="width:90%;" v-model="dialogForm.accountType" placeholder="请输入账户类型" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="accountNum" label="收款账号">
                <el-input style="width:90%;" v-model="dialogForm.accountNum" placeholder="请输入收款账号" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item prop="accountName" label="收款账户名">
                <el-input style="width:90%;" v-model="dialogForm.accountName" placeholder="请输入收款账户名" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item prop="openAddress" label="开户行">
                <el-input style="width:90%;" v-model="dialogForm.openAddress" placeholder="请输入开户行" />
              </el-form-item>
            </el-col>
          </el-row>
          <!-- <el-divider border-style="dashed" />
          <el-row>
            <el-col :span="12">
              <el-form-item prop="password" label="登录密码">
                <el-input style="width:90%" v-model="dialogForm.password" type="password" show-password placeholder="请输入登录密码" />
              </el-form-item>
            </el-col>
          </el-row> -->
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reactive, ref, toRefs } from 'vue'
import hexMD5 from "@/utils/md5.min.js"
import { numFormat, successDeal, warningDeal } from '@/utils/utils'
import api from '../api'
export default {
  setup(props, context) {
    const dialogFormRef = ref(null)

    const state = reactive({
      dialogFormVisible: false,
      dialogForm: {},
      dialogFormRules: {
        showName: [
          { required: true, message: '请输入账户别名', trigger: 'blur' },
        ],
        accountType: [
          { required: true, message: '请输入账户类型', trigger: 'blur' },
        ],
        accountNum: [
          { required: true, message: '请输入收款账号', trigger: 'blur' },
        ],
        accountName: [
          { required: true, message: '请输入收款账户名', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
        ],
      },
    })

    function dialogShow() {
      state.dialogFormVisible = true
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          //校验通过
          const obj = {
            accountName: state.dialogForm.accountName || null,
            accountNum: state.dialogForm.accountNum || null,
            accountType: state.dialogForm.accountType || null,
            openAddress: state.dialogForm.openAddress || null,
            showName: state.dialogForm.showName || null,
            // password: hexMD5(state.dialogForm.password) || null,
          }
          api.accountAdd(obj).then(res => {
            if (res.code == '200') {
              successDeal(res.msg)
              context.emit('dialogClose')
              state.dialogFormVisible = false
            } else {
              warningDeal(res.msg)
            }
          }).catch(err => {
            console.log(err)
          })
        }
      })
    }

    function dialogClose() {
      dialogFormRef.value.resetFields()
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      dialogClose,
      submitHandle,
      dialogShow,
      numFormat,
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-divider--horizontal) {
  margin: 5px 0 !important;
}
:deep(.el-input-group__append) {
  padding: 0 20px !important;
}
</style>